<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>tag管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/style/admin.css" media="all">
</head>

<body>

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字：</label>
                        <div class="layui-input-block">
                            <input type="text" name="keywords" placeholder="可按tagname搜索" autocomplete="off"
                                class="layui-input" style="width: 500px;">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>

            <div class="layui-card-body">
                <table id="LAY-user-table" lay-filter="LAY-user-table"></table>
                <script type="text/html" id="zizeng">
                    {{d.LAY_TABLE_INDEX+1}}
                </script>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"><i
                                class="layui-icon">&#xe654;</i> 添加TAG</button>
                        <button class="layui-btn layui-btn-sm" lay-event="isDel"><i class="layui-icon">&#xe640;</i>
                                    批量删除</button>
                    </div>
                </script>

                <script type="text/html" id="table-seller-admin">
                    {if (buttonAuth('tag/edit'))}
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                            class="layui-icon layui-icon-edit"></i>编辑</a>
                    {/if}
                    {if (buttonAuth('tag/del'))}
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                            class="layui-icon layui-icon-delete"></i>删除</a>
                    {/if}
                </script>
            </div>
        </div>
    </div>

    <script src="/static/layui/layui.all.js"></script>
    <script src="/static/common/js/layTool.js"></script>
    <script src="/static/common/js/jquery.min.js"></script>

    <script>
        layui.config({
            base: '/static/admin/'
        }).use(['table', 'util'], function () {
            var $ = layui.$,
                form = layui.form,
                table = layui.table,
                util = layui.util;

            // 监听搜索
            form.on('submit(LAY-user-back-search)', function (data) {
                var field = data.field;

                // 执行重载
                table.reload('LAY-user-table', {
                    where: field
                });
            });
        });

        renderTable();
        // 渲染表格
        function renderTable() {
            layui.use(['table', 'form', 'layer'], function () {
                var $ = layui.$,
                    form = layui.form,
                    layer = layui.layer,
                    table = layui.table;

                table.render({
                    elem: "#LAY-user-table",
                    url: "/admin/tag/index",
                    cols: [
                        [{
                                type: 'checkbox'
                            },
                            {
                                field: 'zizeng',
                                width: 80,
                                title: '编号',
                                sort: true,
                                templet: '#zizeng',
                                align: 'center'
                            },
                            {
                                field: "tagname",
                                title: "Tag(点击可编辑)",
                                edit: 'text'
                            },
                            {
                                field: "add_time",
                                title: "添加时间"
                            },
                            {
                                title: "操作",
                                align: "center",
                                width: 200,
                                fixed: "right",
                                toolbar: "#table-seller-admin"
                            }
                        ]
                    ],
                    page: !0,
                    limit: 20,
                    toolbar: '#toolbarDemo',
                    height: "full-220",
                    text: "对不起，加载出现异常！"
                });

                 //头工具栏事件
                 table.on('toolbar(LAY-user-table)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    var deList = [];
                    data.forEach(function (n, i) {
                        deList.push(n.id);
                    });
                    if (obj.event == 'isDel') {
                        if (deList.length) {
                            layer.ready(function () {
                                var index = layer.confirm('您确定要删除该Tag？', {
                                    title: '友情提示',
                                    icon: 3,
                                    btn: ['确定', '取消']
                                }, function () {

                                    $.getJSON('{:url("tag/isDelAll")}', {
                                        id: deList
                                    }, function (res) {

                                        if (0 == res.code) {

                                            layer.msg(res.msg);
                                            setTimeout(function () {
                                                renderTable();
                                            }, 300);
                                        } else {
                                            layer.alert(res.msg);
                                        }
                                    });
                                }, function () {

                                });
                            });
                        } else {
                            layer.tips('请选择需要删除的行', $('#LAY-user-table'), {
                                tips: [3, '#5fb878']
                            })
                        }
                    } else if(obj.event == 'add') {
                        layTool.open("{:url('tag/add')}", "添加tag", '50%', '40%');
                    }
                });

                layui.use(['table', 'layer'], function () {
                    let layer = layui.layer;
                    let table = layui.table;

                    table.on("tool(LAY-user-table)",
                        function (e) {
                            if ("del" === e.event) {

                                layer.ready(function () {
                                    var index = layer.confirm('您确定要删除该tag？', {
                                        title: '友情提示',
                                        icon: 3,
                                        btn: ['确定', '取消']
                                    }, function () {

                                        $.getJSON('{:url("tag/del")}', {
                                            id: e.data.id
                                        }, function (res) {
                                            if (0 == res.code) {
                                                layer.msg(res.msg);
                                                setTimeout(function () {
                                                    renderTable();
                                                }, 300);
                                            } else {
                                                layer.alert(res.msg);
                                            }
                                        });
                                    }, function () {

                                    });
                                });
                            } else if ("edit" === e.event) {
                                layTool.open("/admin/tag/edit/id/" + e.data.id, "编辑TAG", '50%', '40%');

                            }
                        });
                });

                //监听单元格编辑
                table.on('edit(LAY-user-table)', function (obj) {
                    var value = obj.value, //得到修改后的值
                        data = obj.data, //得到所在行所有键值
                        field = obj.field; //得到字段
                    //layTool.msg('[ID: ' + data.id + '] ' + field + ' 字段更改值为：' + util.escape(value));
                    $.getJSON('{:url("tag/editTagname")}', {
                        id: obj.data.id,
                        tagname: util.escape(value)
                    }, function (res) {
                        if (0 == res.code) {
                            layer.msg(res.msg);
                            setTimeout(function () {
                                renderTable();
                            }, 300);
                        } else {
                            layer.alert(res.msg);
                        }
                    });
                });

            });
        }
    </script>
</body>

</html>